<template>
  <div class="student">
    <div class="head">
      <div class="head-left">校区管理</div>
      <div class="head-right">
        <el-button type="primary" @click="dialogFormVisible = true"
          >添加校区</el-button
        >
      </div>
      <el-dialog title="添加校区" v-model="dialogFormVisible">
        <el-form :model="form" ref="form">
          <el-form-item label="联系人姓名：">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区名称：">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区电话：">
            <el-input v-model="form.tel" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区手机号：">
            <el-input v-model="form.mobile" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区地址：">
            <el-cascader
              v-model="selectvalue"
              :options="options"
              :props="props"
              @change="handleChange"
              clearable
            ></el-cascader>
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button
              type="primary"
              @click="
                dialogFormVisible = false;
                formadd('form');
              "
              >立即创建</el-button
            >
          </span>
        </template>
      </el-dialog>
    </div>
    <div class="main">
      <el-dialog title="编辑账号" v-model="dialogEdit">
        <el-form :model="form" ref="form">
          <el-form-item label="联系人姓名：">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区名称：">
            <el-input v-model="form.title" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区电话：">
            <el-input v-model="form.tel" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区手机号：">
            <el-input v-model="form.mobile" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="校区地址：">
            <el-cascader
              v-model="selectvalue"
              :options="options"
              :props="props"
              @change="handleChange"
              clearable
            ></el-cascader>
            <el-input v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button
              type="primary"
              @click="
                dialogFormVisible = false;
                FormUpdate('form');
              "
              >立即修改</el-button
            >
          </span>
        </template>
      </el-dialog>
      <el-table :data="campusData" style="width: 100%">
        <el-table-column prop="title" label="校区" width="500">
        </el-table-column>
        <el-table-column prop="name" label="所有人" width="180">
        </el-table-column>
        <el-table-column prop="mobile" label="校区电话" width="180">
        </el-table-column>
        <el-table-column prop="tel" label="校区座机" width="180">
        </el-table-column>
        <el-table-column label="编辑账号" width="180">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="
                dialogEdit = true;
                edit(scope.row.id);
              "
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[1, 5, 10, 20]"
        :current-page="formpage.page"
        :page-size="formpage.psize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { regionData, CodeToText } from "element-china-area-data";
export default {
  name: "index",
  data() {
    return {
      selectvalue: [],
      props: { expandTrigger: "hover" },
      options: regionData,
      formpage: {
        psize: 10,
        page: 1,
      },
      campusData: [],
      dialogFormVisible: false,
      dialogEdit: false,
      form: {
        id: "",
        title: "",
        name: "",
        tel: "",
        mobile: "",
        province: "",
        city: "",
        area: "",
        address: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.campuslist();
  },
  methods: {
    handleChange(value) {
      var loc = "";
      for (let i = 0; i < this.selectvalue.length; i++) {
        loc += CodeToText[this.selectvalue[i]];
      }
      console.log(loc);
      console.log(value);
    },
    // 校区列表
    async campuslist() {
      const { data: res } = await this.$http.get(
        "http://www.eshareedu.cn/edusystems/api/campus/list",
        { params: this.formpage }
      );
      console.log(res);
      this.campusData = res.data.list;
      this.total = res.data.counts;
      this.dialogFormVisible = false;
      this.dialogEdit = false;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.formpage.psize = val;
      this.campuslist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.formpage.page = val;
      this.campuslist();
    },
    // 添加
    async formadd() {
      this.form.province = this.selectvalue[0];
      this.form.city = this.selectvalue[1];
      this.form.area = this.selectvalue[2];
      const { data: res } = await this.$http.post(
        "http://www.eshareedu.cn/edusystems/api/campus/add",
        this.form
      );
      console.log(res);
      if (res.errCode == "10000") {
        this.$message.success("添加成功");
      } else {
        this.$message.error("添加失败");
      }
      this.campuslist();
    },
    // 删除方法
    async del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.get(
            "http://www.eshareedu.cn/edusystems/api/campus/delete",
            { params: { id: id } }
          );
          console.log(res);
          if (res.errCode == "10000") {
            this.$message.success("删除成功!");
          } else {
            this.$message.error("删除失败");
          }
          this.campuslist();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 重置密码
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          const { data: res } = this.$http.post(
            "http://www.eshareedu.cn/edusystems/api/company/update",
            this.ruleForm
          );
          console.log(res);
          // alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },
    async resetpass(id) {
      const { data: res } = await this.$http.get(
        "http://www.eshareedu.cn/edusystems/api/company/get",
        { params: { id: id } }
      );
      console.log(res);
    },
    // 编辑账号
    edit(id) {
      this.form.id = id;
      this.campusData.forEach((item) => {
        console.log(item);
        if (id == item.id) {
          this.form.id = item.id;
          this.form.title = item.title;
          this.form.name = item.name;
          this.form.province = this.selectvalue[0];
          this.form.city = this.selectvalue[1];
          this.form.area = this.selectvalue[2];
          this.form.tel = item.tel;
          this.form.mobile = item.mobile;
          this.form.address = item.address;
        }
      });
    },
    // 执行编辑
    async FormUpdate() {
      const { data: res } = await this.$http.post(
        "http://www.eshareedu.cn/edusystems/api/campus/add",
        this.form
      );
      console.log(res);
      if (res.errCode == "10000") {
        this.$message.success("修改成功");
      } else {
        this.$message.error("修改失败");
      }
      this.campuslist();
    },
  },
};
</script>

<style scoped>
.student {
  min-width: 950px;
  margin-left: 2%;
}

.head {
  width: 98%;
  height: 66px;
  display: flex;
  background-color: #fff;
  border: 2px solid #f5f5f5;
  margin: 25px auto;
  justify-content: space-between;
}

.head .head-left {
  text-align: left;
  padding: 25px;
  color: #333;
}

.head .head-right {
  line-height: 66px;
  padding-right: 20px;
}

.el-button {
  font-size: 12px;
  height: 30px;
}
</style>
